package cn.bx.bid.ctrl;


import cn.bx.bid.entity.Department;
import cn.bx.bid.entity.Page;
import cn.bx.bid.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
//@RequestMapping("Education")
public class DepartmentController {
    @Autowired
    private DepartmentService service;

    @RequestMapping(value = "findDepartments",produces="application/json;charset=UTF-8")
    @ResponseBody
    public List<Department> findDepartments(){
        List<Department> list = this.service.findALL();
        return list;
    }
    //produces = "application/json;charset=UTF-8"   意思返回值强转成JSON
    @RequestMapping(value = "departmentGet", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Department departmentGet(@RequestParam("id") long id) {
        Department professor = this.service.find(id);
        System.out.println("*****************" + professor);
        return professor;
    }

    @RequestMapping(value = "departmentUpdate", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public int departmentUpdate(@ModelAttribute("p") Department p) {
        int update = this.service.update(p);
        return update;
    }


    @RequestMapping(value = "departmentSearch")
    @ResponseBody
    public Object paging(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "departmentName", required = false) String departmentName) {
        Page<Department> page = this.service.paging(pageNo, limit, departmentName);
        return page;
    }

    //u还在上下文中，叫u
    @RequestMapping("departmentAdd")
    @ResponseBody
    public int departmentAdd(@ModelAttribute("p") Department p) {
        int add = this.service.add(p);
        return add;
    }

    @RequestMapping("delALLDepartment")
    @ResponseBody
    public int delALLDepartment(@RequestParam("ids[]") long[] ids) {
        int batchDel = this.service.delALL(ids);
        return batchDel;
    }




    @InitBinder
    public void init(WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }
}
